package com.example.template.ui.community.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.EncodeUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.common.config.AppConfig;
import com.example.template.R;
import com.example.template.http.entity.CommunityEntity;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class CommunityAdapter extends BaseQuickAdapter<CommunityEntity, BaseViewHolder> {


    public CommunityAdapter() {
        super(R.layout.adapter_community_item);
    }


    @Override
    protected void convert(@NotNull BaseViewHolder helper, @NotNull CommunityEntity item) {
        ImageView imageView = helper.findView(R.id.image_avatar);
        Glide.with(imageView).load(AppConfig.getInstance().getConfig().OSS_URL() + "/" + item.getAvatar()).placeholder(com.example.common.R.drawable.avatar).transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop())).into(imageView);
        helper.setText(R.id.tv_name, item.getNickName());
        helper.setText(R.id.tv_content, EncodeUtils.urlDecode(item.getContent()));
//        helper.setText(R.id.tv_content, Html.fromHtml(EncodeUtils.urlDecode(item.getContent())));
        helper.setText(R.id.tv_time, item.getCreated());
        helper.setText(R.id.tv_up, String.valueOf(item.getUp()));
        helper.setText(R.id.tv_reply, String.valueOf(item.getReadNum()));

        List<AttachContent> attachContentList = GsonUtils.fromJson(item.getAttachContent(), new TypeToken<List<AttachContent>>() {}.getType());
        if (attachContentList.size() == 0) return;
        AttachContent attachContent = attachContentList.get(0);
        if (attachContentList.get(0).getType() == 1) {
            helper.setVisible(R.id.layout_link,true);
            helper.setText(R.id.tv_link_content,attachContent.getUrl());
        }
        if (attachContentList.get(0).getType() == 2) {
            helper.setVisible(R.id.recyclerView,true);
            RecyclerView recyclerView = helper.findView(R.id.recyclerView);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
            recyclerView.setLayoutManager(gridLayoutManager);
            recyclerView.setAdapter(new RecyclerView.Adapter() {
                @NonNull
                @Override
                public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    ImageView image = new ImageView(getContext());
                    return new ItemViewHolder(image);
                }

                @Override
                public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                    ImageView image = (ImageView) holder.itemView;
                    Glide.with(image).load(AppConfig.getInstance().getConfig().OSS_URL() + "/" + attachContentList.get(position).getUrl()).placeholder(com.example.common.R.drawable.avatar).transform(new MultiTransformation<>(new CenterCrop(), new RoundedCorners((int) getContext().getResources().getDimension(com.example.base.R.dimen.app_border_radius_base)))).into(image);
                }

                @Override
                public int getItemCount() {
                    return attachContentList.size();
                }
            });
        }
        if (attachContentList.get(0).getType() == 3) {
            helper.setVisible(R.id.videoView,true);
            VideoView videoView = helper.findView(R.id.videoView);
            videoView.setKeepScreenOn(true);
            videoView.setVideoPath(attachContent.getUrl());
//            videoView.setVideoURI(Uri.parse());
            videoView.requestFocus();
            videoView.start();
        }
    }

    public class AttachContent {
        public int type;
        public String url;

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {

        public ItemViewHolder(View itemView) {
            super(itemView);
        }
    }
}
